package cn.edu.sdu.sduhour.controller;

import cn.edu.sdu.sduhour.WechatUtil;
import cn.edu.sdu.sduhour.entity.*;
import cn.edu.sdu.sduhour.mapper.*;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/*
* 说明：用户接口类，用于登录
* 最后更新时间：2022年2月14日
* 最后更新执行人：陈诺
* */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserMapper userMapper;

    @Resource
    StateMapper stateMapper;

    @Resource
    AchivementsMapper achivementsMapper;

    @Resource
    ToolsMapper toolsMapper;

    @Resource
    ReportMapper reportMapper;

    @Resource
    HistoryMapper historyMapper;

    //查询所有用户（管理员）
    /*传入：null*/
    @GetMapping("/all")
    public List<User> findAll() {
        return userMapper.findAll();
    }

    //查询某位用户
    /*传入：String openid;*/
    @PostMapping("/one")
    public List<User> findOne(@RequestBody User user) {
        return userMapper.findByOpenid(user);
    }

    //添加一位用户
    /*传入：
    * String openid;
    * String nickName;
    * String avatarUrl;
    * */
    @PostMapping("/add")
    public List<User> addOne(@RequestBody User user) {
        State state = new State();
        Achivements achivements = new Achivements();
        Tools tools = new Tools();
        Report report = new Report();

        state.setOpenid(user.getOpenid());
        achivements.setOpenid(user.getOpenid());
        tools.setOpenid(user.getOpenid());
        report.setOpenid(user.getOpenid());
        report.setNickName(user.getNickName());
        report.setAvatarUrl(user.getAvatarUrl());
        Random random = new Random();
        int result_num = random.nextInt(2) + 1;
        String result;
        if(result_num == 1) {
            result = "济南";
        }else if(result_num == 2) {
            result = "青岛";
        }else {
            result = "威海";
        }

        Random random1 = new Random();
        int attr_num = random1.nextInt(2) + 1;
        String attribute;
        if(attr_num == 1) {
            attribute = "追光天使";
        }else if(attr_num == 2){
            attribute = "守光神明";
        }else {
            attribute = "光之骑士";
        }
        report.setCity(result);
        report.setAttribute(attribute);

        reportMapper.addOne(report);
        userMapper.addUser(user);
        stateMapper.addOne(state);
        achivementsMapper.addOne(achivements);
        toolsMapper.addOne(tools);
        historyMapper.addOneByUser(user);
        return userMapper.findByOpenid(user);
    }

    //删除一位用户（管理员）
    /*传入：int id;*/
    @DeleteMapping("/del")
    public String delOne(@RequestBody User user) {
        State state = new State();
        Achivements achivements = new Achivements();
        Tools tools = new Tools();
        state.setOpenid(user.getOpenid());
        achivements.setOpenid(user.getOpenid());
        tools.setOpenid(user.getOpenid());
        userMapper.delUser(user);
        stateMapper.delOne(state);
        achivementsMapper.delOne(achivements);
        toolsMapper.delOne(tools);
        historyMapper.delOneByUser(user);
        return "success";
    }

    //微信登陆
    /*传入：String code;*/
    @PostMapping("/login")
    public String getOpenid(@RequestBody Code code) {
//        System.out.println(code);
        String appid="wx32f68a50ede4df7f";
        String secret = "3b7a91037f89e739fa189834303fa9cc";
        String url="https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code.getCode() + "&grant_type=authorization_code";
        String str = WechatUtil.httpRequest(url,"GET",null);
        JSONObject jsonObject = JSONObject.fromObject(str);
        String jsonString = jsonObject.getString("openid");
//        System.out.println(jsonObject);
        return jsonString;
    }
}
